﻿using Model.Origins;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Enums;
using Enums.Sys;
using System.Data;
using DAL.DBUtility;
using Common;
using Model.CodeManage;
using Model.Sys;
namespace DAL.Origins
{
    public class OriginsTempletDAL
    {
        public List<OriginsTempletModel> GetList(string strWhere,string term)
        {
            String sql = @"SELECT oo.*,oe.EnterpriseName FROM Origins_OriginsTemplet oo left join Organization_Enterprise oe
                      on oe.ID=oo.EnterpriseId WHERE 1=1 AND  oo.ViewDel=@ViewDel {0}   and (oe.EnterpriseName like '%" + @term + "%' or oo.Name like'%" + @term + "%')";
            sql = string.Format(sql,strWhere);
            SqlParameter[] parmer =  {
                new SqlParameter("ViewDel",EnumManagement.GetEnumValue(SysEnum.NoDele)),
                 new SqlParameter("@term",term)
           };
            DataTable dt = DbHelperSQL.Query(sql, parmer).Tables[0];
            List<OriginsTempletModel> modelList = ListDataSet.DataSetToIList<OriginsTempletModel>(dt).ToList();
            return modelList;
        }

        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public ResultEntity SaveData(string Content, string title,string imgUrl, SysUserModel userModel)
        {

            ResultEntity resultEntity=new ResultEntity();
            resultEntity = Insert(Content, title,imgUrl, userModel);

            return resultEntity;
        }


        /// <summary>
        /// 根据主键编号获取对应的实体类信息
        /// </summary>
        /// <param name="tempId"></param>
        /// <returns></returns>

        public OriginsTempletModel GetModelById(int tempId)
        {
            OriginsTempletModel model = new OriginsTempletModel();
            string sql = @"select * from Origins_OriginsTemplet  where ID=@ID";
            SqlParameter[] parmars = { new SqlParameter("@ID", PageValidate.ConvertToTrimInt(tempId)) };
            SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, parmars);
            if (sdr.Read())
            {
                model = CommonModelNew(sdr);
            }
            sdr.Close();
            return model;

        }
        private OriginsTempletModel CommonModel(SqlDataReader sdr)
        {
            OriginsTempletModel model = new OriginsTempletModel();
            model.ID = PageValidate.ConvertToTrimInt(sdr["ID"]);
            model.Name = PageValidate.ConvertToTrimString(sdr["Name"]);

            model.EnterpriseId = PageValidate.ConvertToTrimInt(sdr["EnterpriseId"]);

            model.ViewDel = PageValidate.ConvertToTrimInt(sdr["ViewDel"]);

            model.ChangeLoginId = PageValidate.ConvertToTrimInt(sdr["ChangeLoginId"]);
            model.CreateLoginName = PageValidate.ConvertToTrimString(sdr["CreateLoginName"]);
            model.CreateDate = PageValidate.ConvertToDateTime(sdr["CreateDate"]);
            model.CreateLoginId = PageValidate.ConvertToTrimInt(sdr["CreateLoginId"]);
            model.ChangeLoginName = PageValidate.ConvertToTrimString(sdr["ChangeLoginName"]);
            model.ChangeDate = PageValidate.ConvertToDateTime(sdr["ChangeDate"]);
            model.Content = PageValidate.ConvertToTrimString(sdr["Content"]);
            return model;
        }

        private OriginsTempletModel CommonModelNew(SqlDataReader sdr)
        {
            OriginsTempletModel model = new OriginsTempletModel();
            model.ID = PageValidate.ConvertToTrimInt(sdr["ID"]);
            model.Name = PageValidate.ConvertToTrimString(sdr["Name"]);

            model.EnterpriseId = PageValidate.ConvertToTrimInt(sdr["EnterpriseId"]);

            model.ViewDel = PageValidate.ConvertToTrimInt(sdr["ViewDel"]);

            model.ChangeLoginId = PageValidate.ConvertToTrimInt(sdr["ChangeLoginId"]);
            model.CreateLoginName = PageValidate.ConvertToTrimString(sdr["CreateLoginName"]);
            model.CreateDate = PageValidate.ConvertToDateTime(sdr["CreateDate"]);
            model.CreateLoginId = PageValidate.ConvertToTrimInt(sdr["CreateLoginId"]);
            model.ChangeLoginName = PageValidate.ConvertToTrimString(sdr["ChangeLoginName"]);
            model.ChangeDate = PageValidate.ConvertToDateTime(sdr["ChangeDate"]);
            model.Content = PageValidate.ConvertToTrimString(sdr["Content"]);
            model.ImgUrl = PageValidate.ConvertToTrimString(sdr["ImgUrl"]);
            return model;
        }


        /// <summary>
        /// 新建操作
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public ResultEntity Insert(string Content, string title,string imgUrl, SysUserModel userModel)
        {

            string sql = @"INSERT INTO Origins_OriginsTemplet (Name,CreateDate,CreateLoginId,CreateLoginName,ViewDel,Content,EnterpriseId,ImgUrl) VALUES(@Name,@CreateDate,@CreateLoginId,@CreateLoginName,@ViewDel,@Content,@EnterpriseId,@ImgUrl);select @@IDENTITY;";
            SqlParameter[] parmas ={
                                  new SqlParameter("@Name",title),
                                  new SqlParameter("@CreateDate",DateTime.Now),
                                  new SqlParameter("@CreateLoginId",userModel.ID),
                                  new SqlParameter("@CreateLoginName",userModel.LoginName),
                                  new SqlParameter("@ViewDel",EnumManagement.GetEnumValue(SysEnum.NoDele)),
                                  new SqlParameter("@Content",Content),
                                  new SqlParameter("@EnterpriseId",userModel.EnterpriseId),
                                    new SqlParameter("@ImgUrl",imgUrl)
                                  };

            return DbHelperSQL.CommonResult(DbHelperSQL.GetSingle(sql, parmas));
        }
        //不连续
        public int UpdateDigitcode(BindCodeModel model)
        {
            //板数形如  1234/1   1234/2   1234/3   其中1234为表后缀加密过后的信息  / 后面的 1 为第一版

            //string sql = string.Format(@"  update " + AppConst.TablePrefix + MiMa.Jiemi(model.BS.Split('/')[0], AppConst.DigitCodeKey) + " set ZEnterpriseId=" + model.EnterpriseId + ",TempletId={0} where ID in ( select ID from " + AppConst.TablePrefix + MiMa.Jiemi(model.BS, AppConst.DigitCodeKey) + " where BS in({1}))", model.TempletId, model.BS.Split('/')[1]);
            string sql = string.Format(@"  update " + AppConst.TablePrefix + model.BS.Split('/')[0] + " set ZEnterpriseId=" + model.EnterpriseId + ",TempletId={0} where ID in ( select ID from " + AppConst.TablePrefix + model.BS.Split('/')[0] + " where BS in({1}))", model.TempletId, model.BS.Split('/')[1]);

            return DbHelperSQL.ExecuteSql(sql);

        }
        //连续
        public int UpdateDigitcode0(BindCodeModel model)
        {
            //string sql = @"  update "+ AppConst.TablePrefix + MiMa.Jiemi(model.BS.Split('/')[0], AppConst.DigitCodeKey) +" set ZEnterpriseId="+model.EnterpriseId+", TempletId=@TempletId where ID in (select ID from "+ AppConst.TablePrefix + MiMa.Jiemi(model.BS.Split('/')[0], AppConst.DigitCodeKey) +" where BS between @StartBS and @EndBS)";
            string sql = @"  update " + AppConst.TablePrefix + model.StartBS.Split('/')[0] + " set ZEnterpriseId=" + model.EnterpriseId + ", TempletId=@TempletId where ID in (select ID from " + AppConst.TablePrefix + model.StartBS.Split('/')[0] + " where BS between @StartBS and @EndBS)";

            SqlParameter[] parameters ={
                                      new SqlParameter("@TempletId",model.TempletId),
                                     new SqlParameter("@StartBS",model.StartBS.Split('/')[1]),
                                      new SqlParameter("@EndBS",model.EndBS.Split('/')[1]),
                                     };
            return DbHelperSQL.ExecuteSql(sql, parameters);
        }
        //得到最新的一条记录
        public int GetNewId()
        {
            string sql = @" select top 1 ID from Origins_OriginsTemplet order by CreateDate desc ";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            return int.Parse(dt.Rows[0][0].ToString());

        }
        public int UpdateTempletById(int templetId, string content, string title, string imgUrl)
        {
            string sql = @"update Origins_OriginsTemplet set Content=@Content,Name=@Name ,ImgUrl=@ImgUrl  where ID=@ID";
            SqlParameter[] parameters ={
                                      new SqlParameter("@Content",content),
                                      new SqlParameter("@Name",title),
                                        new SqlParameter("@ImgUrl",imgUrl),
                                     new SqlParameter("@ID",templetId),
                                     };
            return DbHelperSQL.ExecuteSql(sql, parameters);
        }
        public DataTable GetTempletById(int templetId)
        {
            string sql = @"select * from Origins_OriginsTemplet where ID =" + templetId;
            return DbHelperSQL.Query(sql).Tables[0];
        }
    }
}
